package org.boulderfoodrescue.webapp.client.pickups.selectors;

import java.util.Comparator;

import org.boulderfoodrescue.webapp.shared.Location;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class LocationSelectorTable extends FlowPanel implements SelectorInterface<Location>{
	protected CellTable<Location> table;
	protected ListDataProvider<Location> dataProvider;
	private Location value;
	public LocationSelectorTable(){
		dataProvider = new ListDataProvider<Location>();
		table = new CellTable<Location>();

		/* Create all the columns */

		ClickableTextCell nameCell = new ClickableTextCell();
		Column<Location, String> nameColumn = new Column<Location, String>(nameCell){
			@Override
			public String getValue(Location object) {
				return object.getName();
			}
		};

		/* Add the columns to the table */
		table.addColumn(nameColumn, "Name");

		dataProvider.addDataDisplay(table);

		ListHandler<Location> sortHandler = new ListHandler<Location>(dataProvider.getList());
		table.addColumnSortHandler(sortHandler);

		/* Set the columns as sortable and add their comparators to the sort handler */
		nameColumn.setSortable(true);
		sortHandler.setComparator(nameColumn, new Comparator<Location>(){
			@Override
			public int compare(Location o1, Location o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});

		final SingleSelectionModel<Location> selectionModel = new SingleSelectionModel<Location>();
		table.setSelectionModel(selectionModel);
		selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				value = selectionModel.getSelectedObject();
			}
		});

		this.add(table);
		table.setWidth("100%");
		//this.setHeight("100%");
		table.setEmptyTableWidget(new HTML("No locations are available to view."));
	}
	@Override
	public Location getValue() {
		return value;
	}
	@Override
	public FlowPanel getTable() {
		return this;
	}
	@Override
	public void setValue(Location value) {
		this.value = value;
	}
	@Override
	public String getValueAsString() {
		if(value == null)
			return "Null value";
		return value.getName();
	}
	@Override
	public void addEntry(Location entry) {
		dataProvider.getList().add(entry);
	}
	@Override
	public void removeEntry(Location entry) {
		dataProvider.getList().remove(entry);
	}
	@Override
	public void refreshEntries() {
		table.redraw();
	}
	
	@Override
	public void clear(){
		dataProvider.getList().clear();
	}
}
